package com.haha.qqchannelbot.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.haha.qqchannelbot.entity.baiduContentReview.BaiduContentReviewVO;
import com.haha.qqchannelbot.entity.baiduContentReview.Datum;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;

public class BaiduContentReviewService {

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    private static MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");


    public static void main(String []args) throws IOException{
        RequestBody body = RequestBody.create(mediaType, "text=1111");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String string = response.body().string();
        System.out.println(string);
        ObjectMapper objectMapper = new ObjectMapper();
        BaiduContentReviewVO baiduContentReviewVO = objectMapper.readValue(string, BaiduContentReviewVO.class);
        System.out.println(baiduContentReviewVO.getConclusionType() == 2);

    }

    public static boolean checkContent(String content) throws IOException {
        RequestBody body = RequestBody.create(mediaType, "text=" + content);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String string = response.body().string();
        System.out.println(string);
        ObjectMapper objectMapper = new ObjectMapper();
        BaiduContentReviewVO baiduContentReviewVO = null;
        try {
            baiduContentReviewVO = objectMapper.readValue(string, BaiduContentReviewVO.class);
            long conclusionType = baiduContentReviewVO.getConclusionType();
            return conclusionType != 1;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String checkContentMsg(String content) throws IOException {
        RequestBody body = RequestBody.create(mediaType, "text=" + content);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String string = response.body().string();
        System.out.println(string);
        ObjectMapper objectMapper = new ObjectMapper();
        BaiduContentReviewVO baiduContentReviewVO = null;
        try {
            baiduContentReviewVO = objectMapper.readValue(string, BaiduContentReviewVO.class);
            long conclusionType = baiduContentReviewVO.getConclusionType();
            if (conclusionType != 1) {
                Datum[] data = baiduContentReviewVO.getData();
                if (data == null) {
                    return null;
                }
                return data[0].getMsg();
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


    /**
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String result = response.body().string();
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = objectMapper.readValue(result, Map.class);
        return map.get("access_token").toString();
    }

}
